package com.zhb.sys.shiro.role.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhb.core.constant.RetCode;
import com.zhb.core.utils.JsonReturn;
import com.zhb.core.utils.UUIDUtil;
import com.zhb.sys.shiro.model.Role;
import com.zhb.sys.shiro.role.service.RoleService;

/**
 * @ClassName: Rolecontroller。
 * @Description: Role管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/role/")
public class RoleController {
	@Autowired
	private RoleService roleService;

	@RequestMapping("roleInfo")
	public ModelAndView roleInfo(Role role, HttpServletRequest request) {
		List<Role> roles = roleService.selectRoleByCondition(role);
		return new ModelAndView("shiro/role/roleInfo").addObject("roleList", roles);
	}

	@RequestMapping("insert")
	public JsonReturn insert(Role role) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		role.setId(UUIDUtil.getId());
		int flag = roleService.insert(role);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
		}
		return jsonReturn;
	}

	@RequestMapping("selectRoleById")
	public JsonReturn selectRoleById(String id, HttpServletRequest request) {
		JsonReturn jsonReturn = new JsonReturn();
		Role role = roleService.selectRoleById(id);
		jsonReturn.setRetcode(RetCode.OK_CODE);
		jsonReturn.setRetmsg(role);
		return jsonReturn;
	}

	@RequestMapping("edit")
	public JsonReturn edit(Role role) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = roleService.update(role);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("修改成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("修改失败");
		}
		return jsonReturn;
	}

	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = roleService.delete(id);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败");
		}
		return jsonReturn;
	}
}